home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10407 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointers!!??
  5. Date: 17 Mar 1996 09:04:50 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ihgniINNck@keats.ugrad.cs.ubc.ca>
  8. References: <4ifhiu$b92@dfw-ixnews1.ix.netcom.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ifhiu$b92@dfw-ixnews1.ix.netcom.com>,
  12. Andrew Heiz  <ishky@ix.netcom.com> wrote:
  13.  >Hi,
  14.  >
  15.  >I have a newbie question about pointers. Will sombody please explain
  16.  >what the advantages of using pointers are. We learned about them in
  17.  >class and programmed with them but we never learned why we were using
  18.  >them. Why would it be more appropriate to use a pointer rather than a
  19.  >variable name? Is it a space/time consideration?
  20.  
  21. Nearly everything is a space/time consideration :) but in this case it's a
  22. more of a power/simplicity tradeoff.
  23.  
  24. One advantage of pointers is that they can be dynamically manufactured at run
  25. time. You call malloc() to get a piece of memory and it gives you a new, unique
  26. pointer. Most non-trivial programs need to be written so that they don't impose
  27. compile-time constraints on the size of data sets---the maximum size of the
  28. data may not even be _known_ accurately beforehand; they need to be able to
  29. dynamically allocate new objects.  Pointers are the method in C for keeping
  30. track of such objects.
  31.  
  32. Another advantage is that pointers can be modified from referring to one object
  33. to refer to another. For example, if you know that a pointer refers to an array
  34. element, you can increment the pointer to refer to the next array element (if
  35. there is one). Or you can use a pointer to walk through a linked data
  36. structure, such as a tree, in which a parent node uses pointers to refer to
  37. children. 
  38.  
  39. Variable names are just compile-time labels, and are not themselves variable.
  40. They are bound to a single object in their scope, and so can't be used for
  41. these kinds of purposes.
  42.  
  43. Finally, computers typically use physical pointers at the machine language
  44. level.  On machines that use pointers (which is by far the majority of
  45. computers), the C pointer abstraction maps onto the machine's representation: a
  46. word which contains a memory address. Code that uses pointers is often more
  47. succint and efficient, though this is not a rule by any means.
  48.  
  49. High-level language programs don't have to use pointers explicitly in order to
  50. benefit from them. Compilers will often use pointers to implement high-level
  51. features which don't involve abstracted pointers. For example, a loop which
  52. goes through an array of integers in Pascal, may still generate optimized
  53. machine code that uses pointers to access the memory.
  54. -- 
  55.  
  56.